﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FinPlusWPFControls
{
    public partial class FinPlusProfileBlock : FinPlusControl
    {
        public string AlphaText  {get { return GetValue(AlphaTextProperty).ToString(); } set { SetValue(AlphaTextProperty, value); } }
        public static readonly DependencyProperty AlphaTextProperty = DependencyProperty.Register("AlphaText", typeof(string), typeof(FinPlusProfileBlock), new UIPropertyMetadata(AlphaTextChangedHandler));

        private Dictionary<char, Brush> _brushs = new Dictionary<char, Brush>();
        private FontFamily _font = new FontFamily("Courier New");
        private int _counter;

        public FinPlusProfileBlock()
        {
            InitializeComponent();
            IsValid = true;
        }
               
        //private 
        private void Set()
        {
            foreach (var c in AlphaText)
            {
                var rectangle = c.Equals('*') ? new Rectangle() { Height = 20, Width = 1, Fill = Brushes.Black }
                    : new Rectangle() { Height = 10, Width = 10, Fill = GetBrush(c) };

                canvas.Children.Add(rectangle);
                Canvas.SetTop(rectangle, 0);
                Canvas.SetLeft(rectangle, _counter);
                _counter  += c.Equals('*')  ? 1 : 10;
            }
        }

        private Brush GetBrush(char c)
        {
            if (!_brushs.ContainsKey(c))
            {
                var brush = c.ToString().Equals(" ") ?  (Brush)Brushes.Transparent:
                         (Brush)new ImageBrush((BitmapImage)FindResource(c.ToString()));

                brush.Freeze();
                _brushs[c] = brush;

                return brush;
            }

            return _brushs[c];
        }

        //events
        public static void AlphaTextChangedHandler(DependencyObject s, DependencyPropertyChangedEventArgs a){((FinPlusProfileBlock)s).Set();}
    }
}
